package com.example.deliveryapplication.wiget

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.WindowManager
import com.example.deliveryapplication.R
import com.example.deliveryapplication.entity.SelectString
import com.example.deliveryapplication.handlerNull
import com.example.deliveryapplication.showToast
import kotlinx.android.synthetic.main.add_client_dialog.*

class AddClientDialog(context: Context): Dialog(context) {
//    private var companyNameTV:TextView?=null
//    private var mConfirmBTN:Button?=null
    private var data:SelectString?=null
    private var listener:CallBack?=null
    private var title:String?=null
    private var isEdit:Boolean=false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.add_client_dialog)
        val lp = window?.attributes
        // 设置窗口宽度为屏幕宽度的80%
        val displayMetrics = context.resources.displayMetrics
        lp?.width = (displayMetrics.widthPixels * 0.8).toInt()
        lp?.height = WindowManager.LayoutParams.WRAP_CONTENT
        lp?.gravity = Gravity.CENTER
        window?.attributes = lp
        mConfirmBTN.setOnClickListener {
            val name=companyNameTV.text.toString()
            if(name.isNullOrEmpty()){
               context.showToast("请先输入公司名称")
            }else{
                listener?.onConfirmClick(name)
                dismiss()
            }
        }
        data?.let {
            companyNameTV.setText(it.name.handlerNull())
        }
        title?.let {
            mTitleTV.text=if(isEdit)"修改$it" else "添加$it"
            leftName.text="${it}名称"
            companyNameTV.hint="请输入${it}名称"
        }
    }
    fun setTitle(title:String){
        this.title=title
    }
    fun setEdit(isEdit:Boolean){
        this.isEdit=isEdit
    }
    fun setListener(listener: CallBack){
        this.listener=listener
    }
    interface CallBack{
        fun onConfirmClick(name:String)
    }
    fun setData(data:SelectString){
        this.data=data
    }
}
